home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / TEST / THREAD_T.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  3.2 KB  |  107 lines

  1. /**
  2.  * This is an example program which shows you how to write multithreaded
  3.  * code safely with sub_arctic.
  4.  */
  5. package sub_arctic.test;
  6.  
  7. import sub_arctic.lib.label;
  8. import sub_arctic.lib.interactor_applet;
  9. import sub_arctic.lib.base_parent_interactor;
  10. import sub_arctic.lib.manager;
  11. import sub_arctic.lib.shadow_drag_container;
  12. import sub_arctic.lib.top_level;
  13. import sub_arctic.input.*;
  14.  
  15. import java.util.Date;
  16.  
  17. public class thread_test extends interactor_applet {
  18.   label my_label;
  19.   public void build_ui( base_parent_interactor top) {
  20.     shadow_drag_container sdc;
  21.     /* set it to say something when the clock is not yet ticking */
  22.     my_label=new label("Starting out");
  23.     /* set the position of the container, not the label */
  24.     sdc=new shadow_drag_container(200,200);
  25.     sdc.add_child(my_label);
  26.     top.add_child(sdc);
  27.   }
  28.   /**
  29.    * Now the interface is built, start your threads.
  30.    */
  31.   public void post_build_ui(base_parent_interactor top) {
  32.     Thread t=new Thread(new time_display("Starting out",my_label));
  33.     t.start();
  34.   }
  35. }
  36. /**
  37.  * This is a little parent class which can pop up a menu.
  38.  */
  39. class time_display implements Runnable, work_proc {
  40.   /* this is what the label is currently displaying */
  41.   String _current_text;
  42.   /* the label to manipulate ... but remember, only manipulate
  43.    * it inside of run_safely().
  44.    */
  45.   label _the_label;
  46.   /* make one of these by initializing the current text */
  47.   public time_display(String t,label l) {
  48.     _current_text=t;
  49.     _the_label=l;
  50.   }
  51.   /**
  52.    * Perform the main code of the thread.
  53.    */
  54.   public void run() {
  55.     Date date;
  56.     /* loop forever */
  57.     while (true) {
  58.       /* sleep for a quarter of a second */
  59.       try {
  60.     Thread.currentThread().sleep(250);
  61.       } catch (InterruptedException e) {
  62.     ; /* don't bother */
  63.       }
  64.       /* create a new date obj */
  65.       date=new Date();
  66.       /* has the time changed? */
  67.       if (_current_text.equals(date.toString())) {
  68.     continue;
  69.       }
  70.       /* set the current text to this date */
  71.       _current_text=date.toString();
  72.       /* it changed, fire off the change */
  73.       manager.perform_work(this,_current_text);
  74.       
  75.     }
  76.   }
  77.   /**
  78.    * This is the code that gets run on the "other side" of the
  79.    * sub_arctic synchronization barrier. This code is allowed to 
  80.    * manipulate the interactors in any way it wants.
  81.    */
  82.   public void run_safely(Object o) {
  83.     /* we know the object passed in is a string */
  84.     String s=(String)o;
  85.     /* update the text on the label to this string*/
  86.     _the_label.set_text(s);
  87.     /* note: you could have just accessed _current_text, but I wanted
  88.      * to demonstrate the parameter passing */
  89.   }
  90. /*=========================== COPYRIGHT NOTICE ===========================
  91.  
  92. This file is part of the subArctic user interface toolkit.
  93.  
  94. Copyright (c) 1996 Scott Hudson and Ian Smith
  95. All rights reserved.
  96.  
  97. The subArctic system is freely available for most uses under the terms
  98. and conditions described in 
  99.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  100. and appearing in full in the lib/interactor.java source file.
  101.  
  102. The current release and additional information about this software can be 
  103. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  104.  
  105. ========================================================================*/
  106.